package com.vdm.action;

import com.vdm.model.*;
import com.vdm.service.*;

import com.vdm.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/dispatch")
public class DispatchController {

    @Autowired
    DispatchService dispatchService;

    @Autowired
    SalesmanService salesmanService;

    @Autowired
    CompanyService companyService;

    @Autowired
    VehicleService vehicleService;

    @Autowired
    PilotService pilotService;

    @Autowired
    AmountService amountService;

    @RequestMapping(value = "/toDispatchList")
    public String toDispatchList(){
        return "dispatch/dispatchList";
    }

    @RequestMapping(value = "/delDispatch")
    public String delDispatch(int id){

        dispatchService.deleteByPrimaryKey(id);

        return "dispatch/dispatchList";
    }

    @ResponseBody
    @RequestMapping(value = "/dispatchList",method = RequestMethod.GET)
    public Map<String,Object> dispatchList(){
        List<DispatchInfo> userList = dispatchService.dispatchInfoList();
        int count = dispatchService.getDispatchInfoCount();
        Map<String,Object> retMap = new HashMap();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value = "/dispatchListByCond",method = RequestMethod.GET)
    public Map<String,Object> dispatchListByCond(Integer status,Integer page,Integer limit,String vehicleNum,Integer companyId,Integer salesmanId,Integer pilotId,String startDateSel){
        //依据时间查出改时间段内需要的所有信息
        Map map = new HashMap();
        page=(page-1)*limit;//页面默认从1开始，，每页个数默认为10条
        map.put("page",page);//从第几页开始
        map.put("limit",limit);//每页显示多少条记录
        map.put("vehicleNum",vehicleNum);
        map.put("companyId",companyId);
        map.put("salesmanId",salesmanId);
        map.put("pilotId",pilotId);
        map.put("status",status);

        if(!StringUtil.isEmpty(startDateSel)){
            String[] strs=startDateSel.split(" - ");
            map.put("startDateSelFrom", strs[0]);
            map.put("startDateSelTo", strs[1]);
        }else{
            map.put("startDateSelFrom",null );
            map.put("startDateSelTo",null );
        }

        List<DispatchInfo> userList = dispatchService.dispatchListByCond(map);
        int count = dispatchService.getDispatchListByCondCount(map);
        Map<String,Object> retMap = new HashMap();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @RequestMapping(value = "/toEditDispatch")
    public String toEditDispatch(int id,HttpServletRequest request){
        DispatchInfo dispatchInfo = dispatchService.selectByPrimaryKey(id);

        request.setAttribute("dispatch",dispatchInfo);
        setInfo(request);

        return "dispatch/editDispatch";
    }

    @ResponseBody
    @RequestMapping(value = "/editDispatch" , method = RequestMethod.POST)
    public Map<String,Object> editDispatch(@RequestBody(required = true) DispatchInfo dispatchInfo){
        if(dispatchService.updateByPrimaryKeySelective(dispatchInfo)>0) {
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    private void setInfo(HttpServletRequest request){
        //业务员
        List<SalesmanInfo> saleList = salesmanService.getAllSalesmanInfo();
        request.setAttribute("saleList",saleList);

        //公司信息
        List<CompanyInfo> companyList = companyService.getAllCompanyInfo();
        request.setAttribute("companyList",companyList);

        //车辆信息
        List<VehicleInfo> vehicleList = vehicleService.getAllVehicleInfo();
        request.setAttribute("vehicleList",vehicleList);

        //驾驶员信息
        List<PilotInfo> pilotList = pilotService.getAllPilotInfo();
        request.setAttribute("pilotList",pilotList);
    }


    @RequestMapping(value = "/toAddDispatch")
    public String toAddDispatch(HttpServletRequest request){
        //生成派车单号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        request.setAttribute("dispatchNum", GenerationNum.getSerialNum("VDM",3));
        request.setAttribute("createTime",sdf.format(new Date()));

        setInfo(request);

        return "dispatch/addDispatch";
    }

    @ResponseBody
    @RequestMapping(value = "/addDispatch" , method = RequestMethod.POST)
    public Map<String,Object> addCompany(@RequestBody(required = true) DispatchInfo dispatchInfo, HttpSession session){
        UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
        dispatchInfo.setCreateBy(user.getLoginName());
        dispatchInfo.setCreateTime(new Date());
        dispatchInfo.setStatus((short) 1);
        System.out.println(dispatchInfo);
        if(dispatchService.insertSelective(dispatchInfo)>0) {
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }


    /*----派车单审核模块----*/
    @RequestMapping(value = "/toAuditDispatchList")
    public String toAuditDispatchList(){
        return "dispatch/auditDispatchList";
    }

    @RequestMapping(value = "/toAuditDispatch")
    public String toAuditDispatch(int id,HttpServletRequest request){
        DispatchInfo dispatchInfo = dispatchService.selectByPrimaryKey(id);

        request.setAttribute("dispatch",dispatchInfo);
        setInfo(request);
        return "dispatch/auditDispatch";
    }

    /*---财务收款模块---*/
    @RequestMapping(value = "/toAmountDispatch")
    public String toAmountDispatch(int id,HttpServletRequest request){
        DispatchInfo dispatchInfo = dispatchService.selectByPrimaryKey(id);
        AmountInfo amount = amountService.selectByDispatchNum(dispatchInfo.getDispatchNum());

        request.setAttribute("amount",amount);
        request.setAttribute("dispatch",dispatchInfo);
        setInfo(request);
        return "dispatch/amountDispatch";
    }

    @ResponseBody
    @RequestMapping(value = "/addAmount" , method = RequestMethod.POST)
    public Map<String,Object> addAmount(@RequestBody AmountInfo amountInfo,  HttpSession session){
        System.out.println("addAmount:"+amountInfo);

        int count = 0;
        if(amountInfo.getId()==null) {
            UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
            amountInfo.setCreateBy(user.getLoginName());
            amountInfo.setCreateTime(new Date());
            count = amountService.insertSelective(amountInfo);
        }else{
            //更新
            count = amountService.updateByPrimaryKeySelective(amountInfo);
        }
        System.out.println(amountInfo);
        if(count > 0) {
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }

    }

    @ResponseBody
    @RequestMapping(value = "/passAmount" , method = RequestMethod.POST)
    public Map<String,Object> passAmount(@RequestBody AmountInfo amountInfo, @RequestParam(value = "status" ,required = false)Integer status,@RequestParam(value = "dispatchId" ,required = false) Integer dispatchId, HttpSession session){
        System.out.println("passAmount:amountINfo:"+amountInfo);

        System.out.println("status:"+status);
        System.out.println("dispatchId:"+dispatchId);

        //更新状态
        DispatchInfo dispatchInfo = new DispatchInfo();
        dispatchInfo.setId(dispatchId);
        dispatchInfo.setStatus(Short.parseShort(status+""));

        int count = 0;
        if(amountInfo.getId()==null) {
            UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
            amountInfo.setCreateBy(user.getLoginName());
            amountInfo.setCreateTime(new Date());
            count = amountService.insertSelective(amountInfo);
        }else{
            //更新
            count = amountService.updateByPrimaryKeySelective(amountInfo);
        }

        dispatchService.updateByPrimaryKeySelective(dispatchInfo);
        System.out.println(amountInfo);
        if(count > 0) {
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

}
